package com.ten.member.contorller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.baomidou.mybatisplus.core.metadata.IPage;

import com.ten.common.core.entity.OssEntity;
import com.ten.common.core.entity.TbMember;
import com.ten.common.core.vo.Result;
import com.ten.member.service.TbMemberService;
import com.ten.member.vo.MemberUpdateVo;
import com.ten.member.vo.MemberVo;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

/**
 * @author ：Wqw
 * @date ：Created in 2022/12/10 0:13
 * @description：会员controller层
 * @modified By：
 * @version:
 */
@RestController
@RequestMapping("/student/member")
@Api(tags = "会员列表API接口类")
public class TbMemberController {

    @Autowired
    private TbMemberService tbMemberService;

    @Autowired
    private OssEntity ossEntity;

    /**
     * 查询会员列表,模糊查询,分页
     * page 页码
     * limit 每页显示行数
     * MemberVo 条件类 用来封装模糊查询和分页
     */
    @ApiOperation(value = "查询会员列表,模糊查询,分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码数"),
            @ApiImplicitParam(name = "limit", value = "当前页显示行数"),
    })
    @PostMapping("/getStudentAll/{page}/{limit}")
    public Result<IPage<TbMember>> getStudentAll(@PathVariable(required = true) Integer page, @PathVariable(required = true) Integer limit,
                                                 @RequestBody MemberVo memberVo) {
        return tbMemberService.getMemberByPage(page, limit, memberVo);
    }


    /**
     * 修改会员信息
     * MemberUpdateVo 条件类
     */
    @ApiOperation(value = "修改会员信息")
    @PostMapping("/modifyStudentInformation")
    public Result<TbMember> modifyStudentInformation(@RequestBody MemberUpdateVo memberUpdateVo) {
        return tbMemberService.updateMemberInfo(memberUpdateVo);
    }


    /**
     * 修改会员状态
     * isDisabled 会员当前状态
     * id 会员id
     */
    @ApiOperation(value = "修改会员状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isDisabled", value = "会员当前状态"),
            @ApiImplicitParam(name = "id", value = "会员id")
    })
    @PostMapping("/stateChange/{isDisabled}/{id}")
    public Result<TbMember> stateChange(@PathVariable Integer isDisabled, @PathVariable String id) {
        return tbMemberService.updateMemberStatus(isDisabled, id);
    }


    /**
     * 使会员type成为老师,通过审核的状态
     * 审核讲师的时候调用该方法
     * 审核通过后,根据当前用户的手机号,使用户成为讲师
     */
    @ApiOperation(value = "使会员type成为老师")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "用户手机号")
    })
    @PostMapping("/beTeacherByDeleted/{mobile}")
    public Result<TbMember> beTeacherByDeleted(@PathVariable String mobile) {
        return tbMemberService.updateTypeByMobile(mobile);
    }


    /************************************前端**************************************/


    /**
     * 通过手机号获取验证码
     * 1，通过手机号查询该用户是否已经存在
     * 2.若该用户不存在，则发送验证码
     */
    @ApiOperation(value = "通过手机号获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "用户手机号")
    })
    @GetMapping("/getYanZhengMa/{mobile}")
    public Result getYanZhengMa(@PathVariable String mobile) {
        return tbMemberService.getYanZhengMaByMobile(mobile);
    }

    /**
     * 注册功能
     **/
    @ApiOperation(value = "注册新用户用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "用户手机号",value = "mobile"),
            @ApiImplicitParam(name = "用户设置的密码",value = "qianPassword")
    })
    @PostMapping("/installMember/{mobile}/{qianPassword}")
    public Result<TbMember> installMember(@PathVariable String mobile,@PathVariable String qianPassword){
        return tbMemberService.insertNewTbMember(mobile,qianPassword);
    }

    /**
     * 个人中心(刚注册的用户信息为空，需要完善)
     * 通过用户手机号查询用户信息
     * 回显到页面*/
    @ApiOperation(value = "完善个人信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "用户手机号",value = "mobile"),
    })
    @GetMapping("/findStudentBymobile/{mobile}")
    public Result<TbMember> findStudentBymobile(@PathVariable String mobile){
        return tbMemberService.selectByMobile(mobile);
    }

    /**
     * 通过手机号获取验证码用来修改密码
     */
    @ApiOperation(value = "通过手机号获取验证码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "mobile", value = "用户手机号")
    })
    @GetMapping("/UpdatePasswordGetYanZhengMa/{mobile}")
    public Result getYanZhUpdatePasswordGetYanZhengMaengMa(@PathVariable String mobile) {
        return tbMemberService.updateYanZhengMaByMobile(mobile);
    }

    /**
     * 修改密码*/
    @ApiOperation(value = "用户修改密码")
    @ApiImplicitParams({@ApiImplicitParam(name = "mobile",value = "登陆手机号"),
            @ApiImplicitParam(name = "newPassword",value = "新密码")})
    @PostMapping("/updateMemberPassword/{mobile}/{newPassword}")
    public Result updateMemberPassword(@PathVariable String mobile,
                            @PathVariable String newPassword){
        return tbMemberService.updatePasswordByMobile(mobile,newPassword);
    }

    /**
     * 根据mobile手机号查询member信息，供课程微服务进行调用
     * @param mobile
     * @return
     */
    @GetMapping("/getMemberByMobile/{mobile}")
    Result<TbMember> getMemberByMobile(@PathVariable String mobile){
        return tbMemberService.getMemberByMobile(mobile);
    }



    /**上传头像*/
    @PostMapping("uploadMsg")
    @ApiOperation(value = "阿里云上传图片请求网址")
    public String uploadMsg(MultipartFile file) {
        String filename = file.getOriginalFilename();

        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(ossEntity.getEndpoint(), ossEntity.getAccessKeyId(), ossEntity.getAccessKeySecret());
        try {
            try {
                String newFileName = UUID.randomUUID() + filename;
                ossClient.putObject(ossEntity.getBucketName(), newFileName, new ByteArrayInputStream(file.getBytes()));
                return "https://" + ossEntity.getBucketName() + "." + ossEntity.getEndpoint() + "/" + newFileName;
            } catch (IOException e) {
                e.printStackTrace();
            }

        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
